layui.config({
    base: '/plugins/module/'
});
layui.use(['table', 'layer','form','cstmodules'], function () {

    var mymod = layui.cstmodules;
    var $ = layui.jquery;
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var mainIndex=null;
    var  url = null;
    var roleTableIns=null;

    var sysmgr={
        param:{
            tableColumn:[
                [
                    {type: 'checkbox'},
                    {field:'id',sort: true,title:'ID',align:'center',width:'100'}
                    ,{field:'name', title:'用户名',align:'center',width:'160'}
                    ,{field:'loginname', title:'账号',align:'center',width:'180'}
                    ,{field:'email', title:'邮箱',align:'center',width:'280'}
                    ,{field:'phonenumber', title:'电话',align:'center',width:'180'}
                    ,{title:'操作', toolbar: '#userRowBar',align:'center',minWidth:'360'}
                ]
            ],
            limit:15,
            limits: [15, 30, 60, 120]
        },

    //删除
    deleteUser:function(data){
        layer.confirm('你确定要删除【'+data.name+'】这用户吗?', {icon: 3, title:'提示'}, function(index){
            $.post("/staff/deleteStaff",{id:data.id},function(res){
                if(res.code==1){

                    table.reload('userTable');
                }
                layer.msg(res.info);
            })
            layer.close(index);
        })
    },

    //批量删除
    batchDelete:function  (){
            //得到选中行
            var checkStatus = table.checkStatus('userTable');
            var dataLength=checkStatus.data.length;
            if(dataLength>0){
                layer.confirm('你确定要删除这些用户数据吗?', {icon: 3, title:'提示'}, function(index){
                    var data=checkStatus.data; //获取选中行的数据
                    var ids="";
                    $.each(data,function(index,item){
                        if(index==0){
                            ids+="ids="+item.id;
                        }else{
                            ids+="&ids="+item.id;
                        }
                    })
                    $.post("/staff/batchDeletestaff",ids,function(res){
                        if(res.code==1){
                            table.reload('userTable');
                        }
                        layer.msg(res.info);
                    })
                    layer.close(index);
                });

            }else{
                layer.msg("请选中操作行")
            }
        },

    //打开添加的弹出层
    openAddLayer:function(data){

        console.log(url);
        mainIndex=layer.open({
            type:1,
            content:$("#addOrUpdateDiv"),
            area:['800px','500px'],
            title:'添加用户',
            skin:"layui-layer-lan",
            success:function(){
                $("#dataFrm")[0].reset();
                url="/staff/addStaff";
               }
            });
        },

    //打开修改的弹出层
        openUpdateMenuLayer:function (data){
            mainIndex=layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['800px','500px'],
                title:'修改用户',
                skin:"layui-layer-lan",
                success:function(){
                    $("#dataFrm")[0].reset();
                    //装载新的数据
                    form.val("dataFrm",data);
                    url="/staff/updateStaff";
                }
            });
    },

    //重置密码
    resetPwd:function(data){
            layer.confirm('你确定要重置【'+data.name+'】这个用户密码吗?', {icon: 3, title:'提示'}, function(index){
                $.post("/staff/resetPwd",{id:data.id},function(res){
                    layer.msg(res.info);
                })
                layer.close(index);
            });
        },

    //打开分配角色的弹出层
    selectRole:function(data){
            mainIndex=layer.open({
                type:1,
                content:$("#selectUserRoleDiv"),
                area:['650px','500px'],
                title:'分配【'+data.name+'】的角色',
                skin:"layui-layer-lan",
                btn: ['<span style="font-size: 15px;"><i class="fa fa-check restbtn"></i>确认分配</span>',
                    '<span style="font-size: 15px "><i class="fa fa fa-times restbtn"></i>关闭窗口</span>'],
                yes: function(index, layero){
                    var checkStatus = table.checkStatus('roleTable');
                    var params="id="+data.id;
                    $.each(checkStatus.data,function(index,item){
                        params+="&ids="+item.id;
                    });
                    $.post("/staff/saveStaffRole",params,function(res){
                        layer.msg(res.info);
                        layer.close(index);

                    })
                },
                btn2: function(index, layero){
                    //return false 开启该代码可禁止点击该按钮关闭
                },
                btnAlign: 'c',
                success:function(){
                    sysmgr.initRoleTable(data);
                }
            })
        },
        //初始化分配角色数据
        initRoleTable:function(data){
            roleTableIns=table.render({
                elem: '#roleTable',
                url:'/staff/initRoleByStaffId',
                where:{
                    id:data.id
                }
                ,cols: [ [
                    {type:'checkbox',align:'center'}
                    ,{field:'id', title:'ID',align:'center'}
                    ,{field:'rolename', title:'角色名称',align:'center'}
                    ,{field:'remark', title:'角色备注',align:'center'}
                ] ]
            })
        },

}

    mymod.initPageTableMethod(table,"userTable","/staff/loadAllStaff",sysmgr.param.tableColumn,"#userToolBar",null,sysmgr.param.limit,sysmgr.param.limits,75);

    //监听工具条
    table.on('tool(userTable)', function (obj) {
        var data = obj.data;
        switch(obj.event){
            case 'delete':
                sysmgr.deleteUser(data);
                break;
            case 'update':
                sysmgr.openUpdateMenuLayer(data);
                break;
            case 'resetPwd':
                sysmgr.resetPwd(data);
                break;
            case 'selectRole':
                sysmgr.selectRole(data);
                break;
        };
    });
    //监听头部工具栏
    table.on('toolbar(userTable)', function(obj){
        var data = obj.data;
        switch(obj.event){
            case 'add':
                sysmgr.openAddLayer();
                break;
            case 'batchdelte':
                sysmgr.batchDelete();
                break;
        };
    });

    //监听提交
    form.on('submit(doSubmit)', function(data){
            console.log(data,url)
        $.post(url,data.field,function(res){
            if(res.code==1){
                table.reload('userTable');
            }
            layer.msg(res.info);
            layer.close(mainIndex);
        })
        return false;
    });

    //模糊查询
    form.on("submit(doSearch)",function(data){
        table.reload('userTable',{
            where:data.field,
            page:{
                curr:1
            }
        });
        return false;
    });

});
